<?php
$caption = BUtils::T('detail_features_lbl');
$this->pageTitle = Yii::app()->name . ' - ' . $caption;

$edit_btn = BUtils::T('edit_btn');
$edit_profile_btn = BUtils::T('edit_profile_btn');

//get url_logo
$url_logo = empty($model->url_logo) ? Yii::app()->request->baseUrl . '/images/logo_default.png' : $model->url_logo;

//define array
$aWeekDaysShort = BUtils::WeekDayKeys(7, true);
$aOpenningHours = BUtils::AllHour24();

//set open hours
$hours_list = '';
if (!empty($hours)) {
	//set open hours display style
	$sWeekBuffer = array();
	foreach ($hours as $v) {
		$sWeekday = $aWeekDaysShort[$v['week_no']];
		$sHours = $aOpenningHours[$v['open_hour']] . ' - ' . $aOpenningHours[$v['close_hour']];
		$sWeekBuffer[$sHours][] = $v['week_no'];//add the current week day
	}
	foreach ($sWeekBuffer as $key => $v) {
		$size = count($v);
		if ($size > 1) $sWeekday = $aWeekDaysShort[$v[0]] . ' - ' . $aWeekDaysShort[$v[$size-1]];
		else $sWeekday = $aWeekDaysShort[$v[0]];
		$hours_list .= '<div class="item"><span class="weekday">'.$sWeekday.'</span><span class="hours">'.$key.'</span></div>';
	}
}

//set the subcategories with category_key
$subcategories = MerchantObject::Subcategories(Yii::app()->user->getState('category_key'));
?>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<div class="container prepend-top append-bottom">
	
	<div class="span-13">
		<div class="box_white append-bottom">
			<div class="biz-form">
				<div class="margin">
					<h3><?php echo BUtils::T('how_profile_tags_work_lbl'); ?></h3>
					<div class="tips"><?php echo BUtils::T('how_profile_tags_work_eg'); ?></div>
				</div>

<?php echo CHtml::form(Yii::app()->createUrl('/merchant/saveTags'), 'post', array('id'=>'merchant-tag-form')); ?>
				<div class="fieldswrap">
<?php if (count($subcategories) > 0) { ?>
<div class="fields">
	<div class="tag-label" id="cuisine_type_lbl"><?php echo BUtils::T('cuisine_type_lbl'); ?></div>
	<div class="tag-list">
		<?php echo CHtml::checkBoxList('cuisine_type', explode(',', $tags['cuisine_type']), $subcategories, array(
			'class'=>'',
			'separator'=>'',
			'template'=>'<span class="fixed-list" style="width:200px;">{input} {label}</span>'
		)); ?>
	</div>
</div>
<?php } ?>
<div class="fields">
	<div class="tag-label"><?php echo BUtils::T('payment_options_lbl'); ?></div>
	<div class="tag-list">
		<?php echo CHtml::checkBoxList('payment_options', explode(',', $tags['payment_options']), BTag::PaymentOptions(), array(
			'class'=>'',
			'separator'=>'',
			'template'=>'<span class="fixed-list">{input} {label}</span>'
		)); ?>
	</div>
</div>
<div class="fields">
	<div class="tag-label"><?php echo BUtils::T('service_languages_lbl'); ?></div>
	<div class="tag-list">
		<?php echo CHtml::checkBoxList('service_languages', explode(',', $tags['service_languages']), BTag::ServiceLanguages(), array(
			'class'=>'',
			'separator'=>'',
			'template'=>'<span class="fixed-list">{input} {label}</span>'
		)); ?>
	</div>
</div>
<div class="fields">
	<div class="tag-label">
		<?php echo BUtils::T('car_parking_lbl'); ?>
	</div>
	<div class="tag-list">
		<?php echo CHtml::checkBoxList('car_parking', explode(',', $tags['car_parking']), BTag::CarParking(), array(
			'separator'=>'',
			'template'=>'<span class="nature-list">{input} {label}</span>'
		)); ?>
	</div>
</div>
				</div><!-- end of .fieldswrap -->
				<div class="margin actions">
					<?php echo CHtml::ajaxSubmitButton(BUtils::T('detail_features_save_btn'), Yii::app()->createUrl($this->id.'/saveTags'), array(
						'dataType' => 'json',
						'beforeSend' => 'js:function(){beforeSubmit("#tags-button");}',
						'success' => 'js:function(data){afterSubmit(data, "#tags-button");}'
					), array('class' => 'button button-huge button-confirm', 'id'=>'tags-button')); ?>
					<?php echo CHtml::button(BUtils::T('cancel_btn'), array('class'=>'button button-huge button-thin', 'onclick'=>'history.go(-1);', 'confirm'=>BUtils::T('cancel_btn_msg'))); ?>
				</div>
<?php echo CHtml::endForm(); ?>
				
			</div>
		</div>
	</div>
	
	<div class="span-11 right last">
		<div class="box_white append-bottom">
			<div class="biz-form">
				<h3><?php echo $model->name; ?></h3>
				<div style="text-align:right;margin-right:7px;">
					<a class="button button-small button-confirm" href="<?php echo Yii::app()->createUrl('/merchant/profile'); ?>"><span><?php echo $edit_profile_btn; ?></span></a>
				</div>
				<div class="fieldswrap">
					<div class="fields mobile">
						<div class="span-3"><a class="pic"><img src="<?php echo $url_logo; ?>" width="100" /></a><br />
							<a href="http://yamii.fi/place/<?php echo Yii::app()->user->id; ?>" target="_blank"><?php echo BUtils::T('view_your_page_lbl'); ?></a>
						</div>
						<div class="span-5 last shop">
							<div class="address"><?php echo $model->address; ?></div>
							<div class="biz_area"><?php echo $model->biz_area; ?></div>
							<div class="city"><?php echo $model->postcode . ' ' . $model->city; ?></div>
							<div class="phone"><?php echo $model->phone; ?></div>
							<div class="link"><?php echo $model->url_site; ?></div>
							<?php if ($model->average_price > 0) { ?>
							<div class="average"><?php echo BUtils::T('average_price_lbl') . ': ' . Yii::app()->locale->numberFormatter->formatCurrency($model->average_price, 'EUR'); ?></div>
							<?php } ?>
						</div>
						<div class="clear"></div>
					</div>
				</div>
				<div class="fieldswrap">
					<div class="fields mobile">
						<div class="fixed-title"><?php echo BUtils::T('opening_hours_lbl'); ?></div>
						<div class="added-hours"><?php echo $hours_list; ?></div>
					</div>
				</div>
<?php if (!empty($tags)) { ?>
				<div class="fieldswrap">
					<div class="fields mobile">
						<div class="fixed-title"><?php echo BUtils::T('detail_features_lbl'); ?></div>
						<div class="tags">
<?php
foreach ($tags as $key => $value) {
	if (!empty($value)) {
		$string = null;
		$array = explode(',', $value);
		foreach ($array as $item) {
			$string .= '<span class="nature-list">' . BUtils::T($item) . '</span>';
		}
		echo '<div style="font-weight:bold;margin-top:10px;">'.BUtils::T($key . '_lbl').':</div><div style="margin-left:40px;">'.$string.'</div>';
	}
}
?>
						</div>
					</div>
				</div>
<?php } ?>
<?php if (!empty($model->brief)) { ?>
				<div class="fieldswrap">
					<div class="fields mobile">
						<div class="fixed-title"><?php echo BUtils::T('merchant_brief_lbl'); ?></div>
						<div class="opening">
							<p><?php echo nl2br($model->brief); ?></p>
						</div>
					</div>
				</div>
<?php } ?>
			</div>
		</div>
		<?php BHelper::HtmlCompletionTips(); ?>
	</div>
</div>

<script type="text/javascript">
$(function() {
	//only select one item
	$("#alcohol_license :checkbox").click(function(){
		var checked = $(this).attr("checked");
		$("#alcohol_license input:checked").removeAttr("checked");
		if (checked) { $(this).attr("checked","checked"); }
	});
});
function beforeSubmit(eId) {
	$(eId).attr('disabled','disabled').addClass('button-disabled');
}
function afterSubmit(data, eId) {
	$(eId).removeAttr('disabled').removeClass('button-disabled');
	if (data.success) {
		alert(data.success);
		location.reload();
	} else {
		for (var ele in data) {
			$('#'+ele).focus();
			return;
		}
	}
}
</script>

